package com.fau.wmbj.server.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import com.fau.wmbj.server.audit.AuditLogMethodArgumentResolver;
import com.fau.wmbj.server.entity.SysConfig;
import com.fau.wmbj.server.interceptor.CustomInterceptor;
import com.fau.wmbj.server.utils.SqlUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 全局配置
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-02-26 16:07
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;
    @Resource
    private AuditLogMethodArgumentResolver auditLogMethodArgumentResolver;
    @Resource
    private SqlUtils sqlUtils;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/files/**") //虚拟url路径
                .addResourceLocations("file:/" + config.getConfigValue()); //真实本地路径

    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token的路由拦截器
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**").excludePathPatterns("/modeler/**", "/");
        // 注册自定义拦截器，这个拦截器用于手动刷新token过期时间
        registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/**").excludePathPatterns("/auth/login", "/upload/**", "/files/**");
    }

    /**
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //允许所有域名进行跨域调用
        config.addAllowedOriginPattern("*");
        //允许跨越发送cookie
        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOriginPatterns("*").allowCredentials(true).allowedMethods("GET", "POST", "DELETE", "PUT").maxAge(3600).exposedHeaders();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(currentUserMethodArgumentResolver);
        resolvers.add(new RequestJsonHandlerMethodArgumentResolver());
        resolvers.add(auditLogMethodArgumentResolver);
    }
}
